$(function () {
    $.ajaxSetup({
        cache: false,
        headers: {
            "header":localStorage.getItem("token")
        }
    });
        $("#tt").datagrid({
            idField : 'id',
            title: "",
            url : basePath + "useMedicine/page",
            beforeSend: function(request) {
                request.setRequestHeader("header", localStorage.getItem("token"));
            },
            width: "100%",
            height: "550px",
            singleSelect : false, /* 是否选中一行 */
            pagination : true,/* 是否显示下面的分页菜单 */
            border:true,
            rowStyler: function() {
                return 'height: 60px';
            },
            onClickRow: function(index,row){
                $("#tt").datagrid("clearSelections");
            },
            rownumbers:true,
            columns : [ [  {field : 'id', checkbox: false,hidden:true},
                {field : 'useTime',title : '时间',width : '15%',align : 'center'},
                {field : 'timePeriod',title : '时间段',width : '10%',align : 'center'},
                {field : 'week',title : '星期',width : '10%',align : 'center'},
                {field : 'content',title : '内容',width : '20%',align : 'center'},
                {field : 'remark',title : '备注',width : '20%',align : 'center'},
                {field : 'op',title : '操作',width : '100',align : 'center',
                    formatter:function(value,row,index){
                        var id = row.id.toString();
                        var del = '<a href="#" onclick="toDelete(\'' + id + '\')">删除</a>';
                        var blank = "&nbsp;&nbsp;|";
                        return del + blank;
                    }
                }

            ]],
            loadMsg : '数据加载中,请稍候......'
        });

        var p = $('#tt').datagrid('getPager');
        $(p).pagination({
            /*
             * 页数文本框前显示的汉字 修改每页默认条数 搜索pageList在jquery.easyui.min.js中修改，
             * 分页区下拉分页数量集合和默认每页分页条数 striped属性 交替行换色
             */
            beforePageText: '第',
            afterPageText: '页    共 {pages} 页',
            displayMsg: '当前显示 {from}-{to} 条记录,共 {total} 条记录'
        });
    }


);

function getPrompt(){
    var input = $("#content").val();
    alert(input);
}



    // 搜索
    function doSearch() {
        $('#tt').datagrid('load', {
            useTime: $('#useTime').val(),
            timePeriod: $('#timePeriod').val()
        });
    }

    function clearQry() {
        $("#qryForm")[0].reset();
        doSearch();
    }

    // 新增框修改框弹出
    function add(){
        var title = "新增";
        $('#addUseMedicineDlg').css("top","0").dialog({
            title: title,
            width: 800,
            height: 400,
            closed: false,
            cache: false,
            href: basePath + 'navigator?page=/useMedicine/add',
            modal: true
        });
    }

    // 新增
    function submitAddUseMedicine(){
        var v = $("#addForm").form("validate");//验证通过为true
        if(!v){
            $.messager.alert('错误提示',"请填写完整信息!",'warning');
            return;
        }
        setBtnDisable("submitAddUseMedicineId");
        loadingProgress();
        $.ajax({
            type:"post",
            url:basePath+"useMedicine/add",
            beforeSend: function(request) {
                request.setRequestHeader("header", localStorage.getItem("token"));
            },
            data:$('#addForm').serializeArray(),
            cache:false,
            dataType: "json",
            success:function(result) {
                closeProgress();
                if(result.result.indexOf("已存在") != -1) {
                    $.messager.alert('warn',result.result,'warning');
                } else {
                    $.messager.show({
                        title:'提示',
                        msg:result.result,
                        timeout:5000,
                        showType:'slide'
                    });
                    cancelDlg("addUseMedicineDlg");
                    $('#tt').datagrid('reload');// 刷新
                }
            }
        });
    }


    // 删除
    function toDelete(id) {

        $.messager.confirm('提示', '你确定要删除这条记录吗?',function(r){
            if(r){
                $.ajax({
                    type:"get",
                    url:basePath + "useMedicine/deleteById/" + id,
                    cache:false,
                    dataType: "json", // this is important
                    success:function(result) {
                        $.messager.show({
                            title:'提示',
                            msg:'删除成功',
                            timeout:5000,
                            showType:'slide'
                        });
                        $('#tt').datagrid('reload');// 刷新
                        rows.length=0;
                    }
                });
            }
        });

    }
    // 关闭弹窗
    function cancelDlg1(){
        $('#addBloodSugarDlg').dialog("close");
    }
    //关闭弹窗
    function cancelDlg(dlgName){
        $('#'+dlgName).dialog("close");
    }
    // 判断是否为空
    function isNull( str ){
        if ( str == "" ) return true;
        var regu = "^[ ]+$";
        var re = new RegExp(regu);
        return re.test(str);
    }


        
        